package com.daisuxia.rongbao;

import com.alibaba.fastjson.JSON;
import com.daisuxia.rongbao.utils.Decipher;
import com.daisuxia.rongbao.utils.HttpClientUtil;
import com.daisuxia.rongbao.utils.Md5Utils;
import com.daisuxia.rongbao.utils.PropertiesUtil;
import com.daisuxia.web.pojo.OutOrders;
import com.daisuxia.web.pojo.Repayment;
import com.daisuxia.web.pojo.User;
import com.daisuxia.web.pojo.UserCardInfo;
import com.daisuxia.web.util.DateUtil;
import com.daisuxia.web.util.GenerateNo;
import com.daisuxia.web.util.JSONUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RongBaoAllUtil {

	private static Logger logger = LoggerFactory.getLogger(RongBaoAllUtil.class);

	private static List moList = new ArrayList();

	static {
		moList.add("3083");
		moList.add("3081");
		moList.add("1101");
	}

	/**
	 * 单笔提交白名单
	 * @return
	 * @throws Exception 
	 */
	public static Map<String, Object> dkWhite(Repayment re, User user, UserCardInfo userCardInfo, String transId, String agreement_no) throws Exception{
	  
	    //商户批次号
	    String merchant_batch_currnum = GenerateNo.payRecordNo();
	    //序号
	    String orderNo = GenerateNo.payRecordNo();
	    
	    //明细内容
//	    1,100000000000147,6214242710498301,韩梅梅,13220482188,身份证,210302196001012114,jk-07253dd,2016-11-04,2017-11-22,jk10333f2302204,测试
//	    1,100000000000147,6225413375128320,韩梅梅,13220482188,身份证,210302196001012114,jk-07253d,2016-11-04,2017-11-22,jk10333f230220411,测试
//	 	   序号(必须)，商户号(必须)，卡号(必须)，姓名(必须)，手机号(必须)，证件类型(必须)，身份证号(必须)，协议号，协议开始日期(必须)，协议结束日期(必须)，商户订单号(必须)，备注
	    
	    RongBaoWhite dkwhite = new RongBaoWhite();
	    dkwhite.setOrder_number(orderNo);//序号
	    dkwhite.setPerson_id(PropertiesUtil.get("merchant_id"));
	    dkwhite.setBank_no(userCardInfo.getCard_no());
	    dkwhite.setAccount_name(user.getRealname());
	    dkwhite.setPhone(user.getUserPhone());
	    dkwhite.setCard_type("身份证");
	    dkwhite.setCard_no(user.getIdNumber());
	    dkwhite.setAgreement_no(agreement_no);
	    dkwhite.setDate_starte(DateUtil.getDateFormat("yyyy-MM-dd"));
	    dkwhite.setDate_end(DateUtil.getDateFormat(DateUtil.getNextMon(1), "yyyy-MM-dd"));
	    dkwhite.setOrder_id(transId);
	    dkwhite.setRemark("测试");
	    String batch_content = dkwhite.getBatch_conten();
	    System.err.println(batch_content);

	    Map<String, String> map = new HashMap<String, String>();
	    map.put("merchant_id", PropertiesUtil.get("merchant_id"));                  //商户号
	    map.put("batch_date", DateUtil.getDateFormat("yyyyMMdd"));                  //提交日期
	    map.put("batch_no", merchant_batch_currnum);                   				//商户批次号
	    map.put("batch_content", batch_content);                                    //明细内容

	    String url_pre = PropertiesUtil.get("url");
	    String url = url_pre + "/singlewhite";
	    
	    Map<String, Object> resMap = new HashMap<>();
	   
			String post;
			
//			post = ReapalSubmit.buildSubmit(map, PropertiesUtil.get("merchant_id"), url);

			//生成签名
			String mysign = Md5Utils.BuildMysign(map, PropertiesUtil.get("key"));
	        map.put("sign", mysign);
	        map.put("sign_type", "MD5");
			String json = JSON.toJSONString(map);
			System.out.println("json:"+json);
			Map<String, String> maps = Decipher.encryptData(json);
			maps.put("merchant_id", PropertiesUtil.get("merchant_id"));
			//发送请求 并接收
			System.out.println(url);
			post = HttpClientUtil.post(url, maps);
			
			//解密返回的数据

			String res = Decipher.decryptData(post);
			System.err.println(res); 
			//打印返回结果
			resMap = JSONUtil.parseJSON2Map(res);
			
			resMap.put("qbm_signParam",json.toString());//请求参数
			resMap.put("payCutType", OutOrders.TYPE_RONGBAO);//交易需要再次查询的错误码
			resMap.put("orderNo", transId);//订单号
			resMap.put("qbm_resultParam",res.toString()); //返回参数
			
			System.out.println("result_code:"+resMap.get("result_code")+",result_msg:"+resMap.get("result_msg"));
	
		return resMap;
	}
	public static Map<String, Object> dkColl(Repayment re, User user, UserCardInfo userCardInfo, String transId, String agreement_no)
					throws Exception{
		
		  //计算出待还金额
		    Long money = (re.getRepaymentAmount() - re.getRepaymentedAmount());
//			明细内容 
//			1,6214242710498301,韩梅梅,建设银行,分行,支行,私,1,CNY,北京,北京,13220482188,身份证,210302196001012114,jk-07253dd1,123r323306,hehe
			
		    RongBaoColl dkColl = new RongBaoColl();
		    dkColl.setOrder_number(GenerateNo.payRecordNo());
		    dkColl.setBank_no(userCardInfo.getCard_no());
		    dkColl.setAccount_name(user.getRealname());
		    dkColl.setBank_name(userCardInfo.getBankName());
		    dkColl.setAccount_type("私");
		    dkColl.setMoney("" + (money / 100.00));
		    dkColl.setMoney_type("CNY");
		    dkColl.setPhone(user.getUserPhone());
		    dkColl.setCard_type("身份证");
		    dkColl.setCard_no(user.getIdNumber());
		    dkColl.setAgreement_no(agreement_no);
		    dkColl.setOrder_id(transId);
		    dkColl.setRemark("融宝支付代扣");
		    
		    //不是必须穿的字段 控制代替
		    dkColl.setBank_fen("");
		    dkColl.setBank_zhi("");
		    dkColl.setProvice("");
		    dkColl.setCity("");
		    String batch_content = dkColl.getBatch_conten();
		    
			    Map<String, String> map = new HashMap<String, String>();
				map.put("merchant_id", PropertiesUtil.get("merchant_id"));        //商户号
				map.put("batch_date", DateUtil.getDateFormat("yyyyMMdd"));        //提交日期
				map.put("batch_no", transId);                   				  //商户批次号 和商户订单号保持一致（便于查账接口）
				map.put("batch_content", batch_content);                          //明细内容
				map.put("batch_count", "1");                                      //批次总数
				map.put("batch_amount", "" + (money / 100.00));                   //批次总金额
				map.put("notify_url", "");   									  //通知地址			
				map.put("batch_version", PropertiesUtil.get("version"));          //版本号
				String url_pre = PropertiesUtil.get("url");
				String url = url_pre+"/single";
				 Map<String, Object> resMap = new HashMap<>();
				
//					String post = ReapalSubmit.buildSubmit(map,PropertiesUtil.get("merchant_id"), url);
					
					 //生成签名
					String mysign = Md5Utils.BuildMysign(map, PropertiesUtil.get("key"));
			        map.put("sign", mysign);
			        map.put("sign_type", "MD5");
					String json = JSON.toJSONString(map);
					//加密数据
					System.out.println("json:"+json);
					Map<String, String> maps = Decipher.encryptData(json);
					maps.put("merchant_id", PropertiesUtil.get("merchant_id"));
					String post = HttpClientUtil.post(url, maps);
					
					//解密返回的数据
					String res = Decipher.decryptData(post);
//					System.err.println(res);
	    			resMap = (Map<String, Object>) JSON.parse(res);

					resMap.put("qbm_signParam",json.toString());//请求参数
	    			resMap.put("payCutType", OutOrders.TYPE_RONGBAO);//交易需要再次查询的错误码
	    			resMap.put("orderNo", transId);//订单号
	    			resMap.put("qbm_resultParam",res.toString()); //返回参数
					resMap.put("resp_code", resMap.get("result_code").toString());
					resMap.put("errorCodes",moList);
//				    System.out.println("result_code:"+resMap.get("result_code")+",result_msg:"+resMap.get("result_msg"));
		logger.info("result_code:"+resMap.get("result_code")+",result_msg:"+resMap.get("result_msg"));
				return resMap;
		}
	public static Map<String ,Object> collQuery(String transId) throws Exception {
		 	Map<String, Object> resMap = new HashMap<>();
		 
		    Map<String, String> map = new HashMap<String, String>();
			map.put("merchant_id", PropertiesUtil.get("merchant_id"));                                         //商户号
			map.put("batch_date", DateUtil.getDateFormat("yyyyMMdd"));                                           //提交日期
			map.put("batch_no", transId);                   //商户批次号
		    map.put("merchant_order_no", transId);                   //商户订单号
			map.put("batch_version", PropertiesUtil.get("version"));                                     //版本号
			String url_pre = PropertiesUtil.get("url");
			String url = url_pre+"/singlequery";
			//生成签名
			String mysign = Md5Utils.BuildMysign(map, PropertiesUtil.get("key"));
			map.put("sign", mysign);
			map.put("sign_type", "MD5");
			String json = JSON.toJSONString(map);
			System.out.println("json:"+json);
			Map<String, String> maps = Decipher.encryptData(json);
			maps.put("merchant_id", PropertiesUtil.get("merchant_id"));
			//发送请求 并接收
			System.out.println(url);
			String post = HttpClientUtil.post(url, maps);
			//解密返回的数据
			String res = Decipher.decryptData(post);
			resMap = (Map<String, Object>) JSON.parse(res);
			resMap.put("qbm_signParam",json.toString());//请求参数
			resMap.put("payCutType", OutOrders.TYPE_RONGBAO);//交易需要再次查询的错误码
			resMap.put("orderNo", transId);//订单号
			resMap.put("qbm_resultParam",res.toString()); //返回参数
			resMap.put("resp_code", resMap.get("result_code").toString());//状态码
			return resMap;
	}
	
	
}
